package com.ruoyi.web.controller.personnel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.personnel.domain.Mechanic;
import com.ruoyi.personnel.service.IMechanicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 技工信息Controller
 *
 * @author ruoyi
 * @date 2021-02-25
 */
@Api("技工信息")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/personnel/mechanic" )
public class MechanicController extends BaseController {

    private final IMechanicService iMechanicService;

    /**
     * 查询技工信息列表
     */
    @ApiOperation("查询技工信息列表")
    @PreAuthorize("@ss.hasPermi('personnel:mechanic:list')")
    @GetMapping("/list")
    public TableDataInfo list(Mechanic mechanic) {
        startPage();
        List<Mechanic> list = iMechanicService.queryList(mechanic);
        return getDataTable(list);
    }

    /**
     * 导出技工信息列表
     */
    @ApiOperation("导出技工信息列表")
    @PreAuthorize("@ss.hasPermi('personnel:mechanic:export')" )
    @Log(title = "技工信息" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Mechanic mechanic) {
        List<Mechanic> list = iMechanicService.queryList(mechanic);
        ExcelUtil<Mechanic> util = new ExcelUtil<Mechanic>(Mechanic.class);
        return util.exportExcel(list, "mechanic" );
    }

    /**
     * 获取技工信息详细信息
     */
    @ApiOperation("获取技工信息详细信息")
    @PreAuthorize("@ss.hasPermi('personnel:mechanic:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iMechanicService.getById(id));
    }

    /**
     * 新增技工信息
     */
    @ApiOperation("新增技工信息")
    @PreAuthorize("@ss.hasPermi('personnel:mechanic:add')" )
    @Log(title = "技工信息" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Mechanic mechanic) {
        mechanic.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iMechanicService.save(mechanic) ? 1 : 0);
    }

    /**
     * 修改技工信息
     */
    @ApiOperation("修改技工信息")
    @PreAuthorize("@ss.hasPermi('personnel:mechanic:edit')" )
    @Log(title = "技工信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Mechanic mechanic) {
        return toAjax(iMechanicService.updateById(mechanic) ? 1 : 0);
    }

    /**
     * 删除技工信息
     */
    @ApiOperation("删除技工信息")
    @PreAuthorize("@ss.hasPermi('personnel:mechanic:remove')" )
    @Log(title = "技工信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(iMechanicService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
